package com.kingyea.app.market.config;

import com.kingyea.app.market.utils.DateUtils;
import org.springframework.format.Formatter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

/**
 * 日期自动转换器
 *
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2018/01/01 15:45
 */
@Component
public class DateAutoFormatter implements Formatter<Date> {


    // 显示Formatter<T>的T类型对象  
    @Override
    public String print(Date date, Locale locale) {
        return DateUtils.date2String(date, DateUtils.YYYYMMDD_SLASH_LONG);
    }

    // 解析文本字符串返回一个Formatter<T>的T类型对象。  
    @Override
    public Date parse(String source, Locale locale) throws ParseException {
        try {
            return DateUtils.string2Date(source);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }
}  